/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *		1、2014-11-24，tfl创建。 
 */
package com.chinamobile.ln.ictenterprises.ui.activity;

import java.util.ArrayList;
import java.util.List;
import com.chinamobile.ln.ictenterprises.R;
import com.chinamobile.ln.ictenterprises.common.MainApplication;
import com.chinamobile.ln.ictenterprises.ui.adapter.FragmentAdapter;
import com.chinamobile.ln.ictenterprises.ui.fragment.OverSendFragment;
import com.chinamobile.ln.ictenterprises.ui.fragment.TimerSendFragment;
import com.chinamobile.ln.ictenterprises.utils.AlertDialogUtil;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HistoryActivity extends FragmentActivity implements
    OnClickListener
{
    private MainApplication app;
    private List<Fragment> list;
    private FragmentAdapter adapter;
    private ViewPager mViewPager;
    private int currIndex; // 当前页卡编号
    private RelativeLayout ly_send_over, ly_send_timer, ly_guide;
    private TextView send_over_txt, send_timer_txt;
    private ImageView guideImageView;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        app = MainApplication.getInstance();
        setContentView(R.layout.history_layout);
        sharedPreferences = getSharedPreferences("firststart",
            Context.MODE_PRIVATE);
        setupView();
    }

    protected void setupView()
    {
        if (sharedPreferences.getBoolean("firststart", true))
        {
            Editor editor = sharedPreferences.edit();
            editor.putBoolean("firststart", false);
            editor.commit();
            ly_guide = (RelativeLayout) findViewById(R.id.ly_guide);
            ly_guide.setVisibility(View.VISIBLE);
            ly_guide.getBackground().setAlpha(100);
            guideImageView = (ImageView) findViewById(R.id.ok);
            guideImageView.setOnClickListener(this);
        }
        TextView topTitleView = (TextView) findViewById(R.id.tv_top_title);
        ly_send_over = (RelativeLayout) findViewById(R.id.ly_send_over);
        ly_send_timer = (RelativeLayout) findViewById(R.id.ly_send_timer);
        ly_send_over.setOnClickListener(this);
        ly_send_timer.setOnClickListener(this);
        send_over_txt = (TextView) findViewById(R.id.tv_send);
        send_timer_txt = (TextView) findViewById(R.id.tv_timer);
        topTitleView.setText(R.string.history);
        LinearLayout btnMenu = (LinearLayout) findViewById(R.id.btn_top_menu);
        btnMenu.setOnClickListener(this);
        list = new ArrayList<Fragment>();
        list.add(new OverSendFragment());
        list.add(new TimerSendFragment());
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        adapter = new FragmentAdapter(getSupportFragmentManager(), list);
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0)
            {
                currIndex = arg0;
                if (currIndex == 0)
                {
                    app.getMenu().setSlidingEnabled(true);
                    send_over_txt.setTextColor(getResources().getColor(
                        R.color.white));
                    send_timer_txt.setTextColor(getResources().getColor(
                        R.color.blue));
                    ly_send_over
                        .setBackgroundResource(R.drawable.table_sel_left);
                    ly_send_timer
                        .setBackgroundResource(R.drawable.table_bg_right);
                }
                else
                {
                    app.getMenu().setSlidingEnabled(false);
                    send_timer_txt.setTextColor(getResources().getColor(
                        R.color.white));
                    send_over_txt.setTextColor(getResources().getColor(
                        R.color.blue));
                    ly_send_timer
                        .setBackgroundResource(R.drawable.table_sel_right);
                    ly_send_over
                        .setBackgroundResource(R.drawable.table_bg_left);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {

            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {

            }
        });
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
        case R.id.btn_top_menu:
            app.getMenu().setSlidingEnabled(true);
            if (!app.getMenu().isShown())
            {
                app.getMenu().showContent();
            }
            else
            {
                mViewPager.setCurrentItem(0);
                app.getMenu().showMenu();
            }
            break;
        case R.id.ly_send_over:
            mViewPager.setCurrentItem(0);
            break;
        case R.id.ly_send_timer:
            mViewPager.setCurrentItem(1);
            break;
        case R.id.ok:
            ly_guide.setVisibility(View.GONE);
            break;
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if (list != null)
        {
            list.clear();
            list = null;
        }
        adapter = null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
        {
            AlertDialogUtil.exit(this, R.string.quit_title, R.string.quit_msg);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
